﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using ObjectBakery.Tests.TestFactories;
using ObjectBakery.Helpers;
using ObjectBakery.Tests.TestDoubles;
using System.Data;
using System.Collections;

namespace ObjectBakery.Tests
{
    /// <summary>
    /// Summary description for IntegrationTests
    /// </summary>
    [TestClass]
    public class IntegrationTests
    {
        public IntegrationTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

   



        [TestMethod]
        public void ExcersiseCodeGenProxyAndSync()
        {

            var dataTableProducts = new TestTableFactory().BuildProducts();
            var dataTableAssets = new TestTableFactory().BuildAssets();

            StringBuilder srcBuilder = new StringBuilder();

            var productEntityGenerator = new CodeGenerator(dataTableProducts, "ObjectBakery.Tests.TestDoubles.Product");
            var assetEntityGenerator = new CodeGenerator(dataTableAssets, "ObjectBakery.Tests.TestDoubles.Asset");

           //Note: you must provide the full spool describing all classes, interfaces, enums, and structs

            srcBuilder.AppendLine(productEntityGenerator.GenerateCode());
            srcBuilder.AppendLine(assetEntityGenerator.GenerateCode());

            //loading this enum into script manually for now.
            srcBuilder.AppendLine(@"namespace ObjectBakery.Tests.TestDoubles
                                    {
                                        public enum AssetFormat
                                        {
                                            Jpg,
                                            Png,
                                            Eps,
                                            PDF,
                                            Docx,
                                            Doc,
                                            Html,
                                            Unspecified
                                        }
                                    }");


            string srcCode = srcBuilder.ToString();

            var productObjectProxy = new ScriptProxy("ObjectBakery.Tests.TestDoubles.Product", 
                                    srcCode, 
                                    Language.CSharp);
            //var assetObjectProxy = new ObjectProxy("ObjectBakery.Tests.TestDoubles.Asset",
            //                        srcCode,
            //                        Language.CSharp);

            var shellProduct = new Product();

            var syncHelper = new DataSyncHelper();

            //out -> in
            syncHelper.Sync(productObjectProxy.ProxiedObject, shellProduct);

            shellProduct.Name = "Blah";

            //in -> out
            syncHelper.Sync(shellProduct, productObjectProxy.ProxiedObject);


            Assert.AreEqual(shellProduct.Name, 
                            productObjectProxy.GetProperty("Name"));

        
        }


        [TestMethod]
        public void ExcersiseCreateProxiesAndSyncIn()
        {
            var syncHelper = new DataSyncHelper();

            var dataTableProducts = new TestTableFactory().BuildProducts();
            var dataTableAssets = new TestTableFactory().BuildAssets();

            StringBuilder srcBuilder = new StringBuilder();

            var productEntityGenerator = new CodeGenerator(dataTableProducts, "ObjectBakery.Tests.TestDoubles.Product");
            var assetEntityGenerator = new CodeGenerator(dataTableAssets, "ObjectBakery.Tests.TestDoubles.Asset");

            //Note: you must provide the full spool describing all classes, interfaces, enums, and structs

            srcBuilder.AppendLine(productEntityGenerator.GenerateCode());
            srcBuilder.AppendLine(assetEntityGenerator.GenerateCode());

            //loading this enum into script manually for now.
            srcBuilder.AppendLine(@"namespace ObjectBakery.Tests.TestDoubles
                                    {
                                        public enum AssetFormat
                                        {
                                            Jpg,
                                            Png,
                                            Eps,
                                            PDF,
                                            Docx,
                                            Doc,
                                            Html,
                                            Unspecified
                                        }
                                    }");


            string srcCode = srcBuilder.ToString();


            var assetObjectProxy = new ScriptProxy("ObjectBakery.Tests.TestDoubles.Asset",
                                    srcCode,
                                    Language.CSharp);

            var productObjectProxy = new ScriptProxy("ObjectBakery.Tests.TestDoubles.Product",
                                    srcCode,
                                    Language.CSharp);


            ArrayList remoteObjects = new ArrayList();


            //ObjectAdapter objectAdapter;

            //objectAdapter = new ObjectAdapter(assetObjectProxy.ProxiedObject.GetType(), dataTableAssets);
            //objectAdapter.Fill(remoteObjects);

            //Assert.IsTrue(remoteObjects.Count > 0);


            //NOTE: This only works on Asset Type right now.  
            //Product won't work.  It holds a reference to the Asset Def that although shares exact full path, isn't same, because from a diff assembly.


            
            //remoteObjects.Clear(); //reset
            //objectAdapter = new ObjectAdapter(productObjectProxy.ProxiedObject.GetType(), dataTableProducts);
            
            ////need cross assembly coersion

            ////Now will fail.. Find solution for this..
            //objectAdapter.Fill(remoteObjects);
            //Assert.IsTrue(remoteObjects.Count > 0);

            Assert.Inconclusive("Finish Me..");
        }
    }
}
